package com.hf.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hf.entity.Myaddress;
import com.hf.utils.DBUtils;

public class AddressDao {
	
	public List<Myaddress> getAddressAll(int user_id){
		
		String sql = "select * from my_address where user_id=?";
		
		Connection conn =null;
		ResultSet rs =null;
		PreparedStatement ps = null;
		List<Myaddress> addresslist = new ArrayList<Myaddress>();
		try{
			conn =  DBUtils.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1,user_id);
			rs = ps.executeQuery();
			Myaddress myaddress = null;
			while(rs.next()){
				myaddress = new Myaddress();
				myaddress.setAddress_id(rs.getInt("address_id"));
				myaddress.setUser_id(rs.getInt("user_id"));
				myaddress.setConsignee(rs.getString("consignee"));
				myaddress.setRegion_province(rs.getString("region_province"));
				myaddress.setRegion_city(rs.getString("region_city"));
				myaddress.setRegion_area(rs.getString("region_area"));
				myaddress.setDetailed_add(rs.getString("detailed_add"));
				myaddress.setZip_code(rs.getString("zip_code"));
				myaddress.setAddress_telephone(rs.getString("address_telephone"));
				addresslist.add(myaddress);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, rs, null, ps);
		}
		return addresslist;
	}
	
	public void insertAdd(Myaddress myaddress){
		
		String sql = "insert into my_address(user_id,consignee,region_province,region_city," +
				"region_area,detailed_add,zip_code,address_telephone) values (?,?,?,?,?,?,?,?)";	
		
		Connection conn =null;
		ResultSet rs =null;
		PreparedStatement ps = null;
		
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, myaddress.getUser_id());
			ps.setString(2, myaddress.getConsignee());
			ps.setString(3, myaddress.getRegion_province());
			ps.setString(4, myaddress.getRegion_city());
			ps.setString(5, myaddress.getRegion_area());
			ps.setString(6, myaddress.getDetailed_add());
			ps.setString(7, myaddress.getZip_code());
			ps.setString(8, myaddress.getAddress_telephone());
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, rs, null, ps);
		}
	}
	
}
